<template>
  <div class="login-container">
    <div class="login-box"><!-- 在一个背景容器里面再定义一个容器 -->
      <div style="padding: 30px;background-color: white;margin-left: 225px;margin-bottom: 80px;box-shadow: 0 0 10px rgba(0,0,0,0.5)">
      <el-form :rules="data.rules" ref="formRef" :model="data.form" style="width: 300px" >
        <div style="margin-bottom: 30px; font-size: 24px; color: #0066bc;font-weight: bold;text-align: center">欢迎登录后台管理系统</div>
        <el-form-item prop="username">
          <el-input size=large v-model="data.form.username" placeholder="请输入账号" prefix-icon="User"></el-input><!-- User需要导入一下 -->
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password size=large v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input><!-- Lock需要导入一下 -->
        </el-form-item>
        <el-form-item prop="role">
          <el-select v-model="data.form.role" style="width: 100%;" size="large">
            <el-option value="ADM" label="管理员"></el-option>
            <el-option value="STU" label="学生"></el-option>
            <el-option value="PRE" label="社长"></el-option>
          </el-select>
        </el-form-item>
        <div style="margin-bottom: 20px">
          <el-button @click="login" size="large" style="width: 100%" type="primary">登录</el-button>
        </div>
        <div style="text-align: right">还没有账号？请 <a style="color: blue;text-decoration: none" href="/register">注册</a></div>
      </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import {User, Lock} from "@element-plus/icons-vue"
import {ElMessage} from "element-plus";
import request from "@/utils/request.js";

const data = reactive({
  form: {role: 'ADM'}, // role 默认为管理员
  rules: {
    username: [
      {required: true, message: '请输入账号', trigger: 'blur'}
    ],
    password: [
      {required: true, message: '请输入密码', trigger: 'blur'}
    ]
  }
})

const formRef = ref()

const login = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/login', data.form).then(res => {
        if (res.code === '200') {
          localStorage.setItem('xm-pro-user', JSON.stringify(res.data))
          ElMessage.success('登录成功')
          setTimeout(() => {
            const role = res.data.role
            if (role === 'ADM') {
              location.href = '/admin/admin-manage'
            } else if (role === 'STU') {
              location.href = '/student/home'
            } else if (role === 'PRE') {
              location.href = '/president/clubs-all'
            } else {
              ElMessage.error('未知角色，无法跳转')
            }
          }, 500)
        } else {
          ElMessage.error(res.msg || '登录失败，请检查账号密码')
        }
      }).catch(error => {
        console.error('登录失败:', error)
        ElMessage.error(error.msg || '登录失败，请稍后重试')
      })
    }
  })
}
</script>

<style scoped>
.login-container{
  height: 100vh;/*vh是明确高度*/
  overflow: hidden;/*超过的页面隐藏掉*/
  background-image: url("@/assets/Login.png");/*导入背景图片*/
  background-size: cover;
  background-position: 0 -15px;
}
.login-box{
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  position: absolute;
  left: 50%;              /* 相对父容器左移50% */
  top: 50%;               /* 相对父容器上移50% */
  transform: translate(-50%, -50%); /* 自身宽高回退50% */
}
</style>